//
//  CCYStatusListVM.swift
//  03.微博
//
//  Created by 蔡承毅 on 16/9/28.
//  Copyright © 2016年 IOS. All rights reserved.
//

import UIKit

class CCYStatusListVM: NSObject {
    static let shared: CCYStatusListVM = CCYStatusListVM()
    
    fileprivate override init() {
        super.init()
    }
    
    /// 加载微博数据
    func loadStatus(completion: @escaping ([CCYStatusVM]?, Error?) -> Void) -> Void {
        guard let access_token = CCYUserAccountVM.shared.userAccount?.access_token else {
            print("access_token 没有值")
            return
        }
        
        let urlSting = "statuses/home_timeline.json"
        
        let parameters = [
            "access_token": access_token
        ]
        
        CCYNetworkTool.shared.request(method: .GET, URLString: urlSting, parameters: parameters, progress: nil, success: { (_, responseObject) in
            // 创建数组
            var statusVMs = [CCYStatusVM]()
            
            if let dict = responseObject as? [String: Any],
                let statusArray = dict["statuses"] as? [[String: Any]]{
                for dict in statusArray{
                    let status = CCYStatus(dict: dict)
                    
                    let statusVM = CCYStatusVM(status: status)
//                    print("获取网络成功：\(responseObject)")
                    statusVMs.append(statusVM)
                }
            }
            completion(statusVMs,nil)
            }) { (_, error) in
                completion(nil,error)
        }
    }
}
